package com.nidu.demo.web.system;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.UserTypeEnum;
import com.nidu.demo.notify.api.SysNotifyMessageService;
import com.nidu.demo.notify.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 站内信消息管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 站内信消息")
@RestController
@RequestMapping("/system/notify-message")
@Validated
@RequiredArgsConstructor
public class SysNotifyMessageController {

    private final SysNotifyMessageService notifyMessageService;

    @Operation(summary = "发送站内信", description = "发送站内信消息")
    @PostMapping("/send")
    @PreAuthorize("@ss.hasPermission('system:notify-message:send')")
    public Response sendNotifyMessage(@RequestBody NotifyMessageSendCmd cmd) {
        return notifyMessageService.send(UserTypeEnum.ADMIN, cmd);
    }

    @Operation(summary = "批量发送站内信", description = "批量发送站内信消息")
    @PostMapping("/batch-send")
    @PreAuthorize("@ss.hasPermission('system:notify-message:send')")
    public Response batchSendNotifyMessage(@RequestParam("userIds") Set<Long> userIds,
                                         @RequestBody NotifyMessageSendCmd cmd) {
        return notifyMessageService.send(UserTypeEnum.ADMIN, userIds, cmd);
    }

    @Operation(summary = "标记站内信为已读", description = "根据 ID 标记站内信为已读")
    @PutMapping("/{id}/read")
    @PreAuthorize("@ss.hasPermission('system:notify-message:update')")
    public Response updateReadStatus(@PathVariable Long id) {
        return notifyMessageService.updateReadStatus(id);
    }

    @Operation(summary = "批量标记站内信为已读", description = "批量标记站内信为已读")
    @PostMapping("/batch-read")
    @PreAuthorize("@ss.hasPermission('system:notify-message:update')")
    public Response batchUpdateReadStatus(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return notifyMessageService.updateReadStatus(new HashSet<>(ids));
    }

    @Operation(summary = "删除站内信", description = "根据 ID 删除站内信")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notify-message:delete')")
    public Response deleteNotifyMessage(@PathVariable Long id) {
        return notifyMessageService.delete(id);
    }

    @Operation(summary = "批量删除站内信", description = "根据 ID 列表删除多个站内信")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:notify-message:delete')")
    public Response deleteNotifyMessages(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return notifyMessageService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取站内信详情", description = "根据 ID 获取站内信详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notify-message:query')")
    public SingleResponse<NotifyMessageCO> getNotifyMessageById(@PathVariable Long id) {
        return notifyMessageService.getById(id);
    }

    @Operation(summary = "分页查询站内信", description = "分页获取站内信信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:notify-message:query')")
    public PageResponse<NotifyMessageCO> pageNotifyMessages(NotifyMessagePageQry qry) {
        return notifyMessageService.page(qry);
    }

}
